#include "list.h"

node *jose_create(int n)
{
	//1.首先创建头结点,开始的比较特殊
	int i = 1;
	node *p, *newnode, *current;
	p = malloc(sizeof(node)); //p是头指针
	if (p == NULL)
		return NULL;
	p->data = i++;
	p->next = p;   //成环
	current = p;
	//其余结点插入
	for (; i <= n; i++)
	{
		newnode = malloc(sizeof(*newnode));
		if (newnode == NULL)
			return NULL;
		newnode->data = i;
		newnode->next = p;
		current->next = newnode;
		current = newnode;
	}
	return p;
}

void jose_show(node *first)
{
	node *current = first;
	while (current->next != first)
	{
		printf("%d ", current->data);
		current = current->next;
	}
	printf("%d\n", current->data);
}

//void jose_show(node *first)
//{
//	node *list = first;
//	while (list->next != first)
//	{
//		printf("%d ", list->data);
//		list = list->next;
//	}
//	printf("%d\n", list->data);  //打印最后一个结点/打印只有一个结点的环链
//}

//因为在删除时可能要修改头指针，所以要传二级指针
void jose_kill(node **first, int n)
{
	node *current = *first, *pre = NULL;
	int i = 1;
	while(current->next != current)
	{
		while (i++ != n)
		{
			pre = current;
			current = current->next;
		}
		pre->next = current->next;
		printf("%d ",current->data);
		free(current);
		current = pre->next;
		i = 1;
	}
	*first = current;
	printf("\n");
}
//(老师的)
//void jose_kill(node **first, int n)
//{
//	//找到被删结点的前驱就停下
//	node *current = *first;
//	node *pre = NULL;
//	int i = 1;
//	while (current != current->next)
//	{
//		while (i < n)
//		{
//			pre = current;
//			current = current->next;
//			i++;
//		}
//		printf("%d ", current->data);
//		pre->next = current->next;
//		free(current);
//		current = pre->next;
//		i = 1;
//	}
//	*first = current; //最后要修改first指针的指向
//	printf("\n");
//}